/*
 * CS_SOCIAL_REQUEST_UNREGISTER.cpp
 *
 *  Created on: 2013. 5. 27.
 *      Author: the2
 */


#include "../../Command.h"
#include "../../User.h"
#include "../../Proxy.h"
#include "../../../../protocol/iFriends.h"
#include "../../NLogic/Space.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_SOCIAL_REQUEST_UNREGISTER(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	UnregisterSyn msg;
	::ProjectZ::NProxy::SendFriendsServer(pUser->GetUserSeq(), msg);

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::S_PROCEDURE);
	clsAuto->MakeQuery("call sp_unregister_user('%s')", pUser->GetSocialID().c_str());

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		* pAck << static_cast<int16_t>(ACK_DB_ERROR);

	if( clsAuto->GetErrorIdx() != 0 )
		* pAck << static_cast<int16_t>(ACK_DB_ERROR);
	else
		* pAck << static_cast<int16_t>(ACK_OK);

	pAck->MakePacket(CMD_SC_SOCIAL_REQUEST_UNREGISTER);
	pUser->Send(pAck);

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL != pSpace)
		pSpace->LeaveUser(pUser);

	pUser->FlushCharacterInfo();
}

} /*NCommand*/ } /*ProjectZ*/
